#include "../Global/GlobalDefs.hpp"
#ifdef I2C_TEMP_SENSOR
#include "tempUpdate.hpp"
#include "../Global/globalVars.hpp"
#include <arduino.h>
#include "Wire.h"
#include <driver/i2c.h>

#include "TMP102/Temperature_LM75_Derived.h"
#include "TMP116119/ClosedCube_TMP116.h"

#define DRIVER_TEMP_ADDR0           0x48
#define DRIVER_TEMP_ADDR1           0x49
#define DRIVER_TEMP_ADDR2           0x4a
#define DRIVER_TEMP_ADDR3           0x4b

#define SAMPLE_TEMP_CENTER_ADDR					0x48	// 00
#define SAMPLE_TEMP_BOTTOM_BOARD_ADDR	0x49	// 01
// on the left when viewed from face of heat bed. On the right from birdview
#define SAMPLE_TEMP_BIRDVIEW_RIGHT_ADDR						0x4a	// 10
// on the right when viewed from face of heat bed. On the left from birdview
#define SAMPLE_TEMP_BIRDVIEW_LEFT_ADDR					0x4b	// 11

uint8_t sensorInit = 0;
uint8_t driverStates[4] = { 0,0,0,0 };
TI_TMP102_Compatible TECDrv1(&Wire1, DRIVER_TEMP_ADDR0);
TI_TMP102_Compatible TECDrv2(&Wire1, DRIVER_TEMP_ADDR1);
TI_TMP102_Compatible TECDrv3(&Wire1, DRIVER_TEMP_ADDR2);
TI_TMP102_Compatible HeatDrv(&Wire1, DRIVER_TEMP_ADDR3);

ClosedCube::Sensor::TMP116 SampleCenter(SAMPLE_TEMP_CENTER_ADDR);
ClosedCube::Sensor::TMP116 SamplePump(SAMPLE_TEMP_BOTTOM_BOARD_ADDR);
ClosedCube::Sensor::TMP116 SampleLeft(SAMPLE_TEMP_BIRDVIEW_LEFT_ADDR);
ClosedCube::Sensor::TMP116 SampleRight(SAMPLE_TEMP_BIRDVIEW_RIGHT_ADDR);
//0000 0001 0010 0000

bool initADCIfNeeded() {
	if (sensorInit > 0) {
		return true;
	}
	bool ret = false;

	Wire1.begin(DRIVER_TEMP_SDA, DRIVER_TEMP_SCL);
	Wire.begin(SAMPLE_TEMP_SDA, SAMPLE_TEMP_SCL);
	pinMode(INTERNAL_TEMP_ADC_PIN, INPUT);
	analogSetPinAttenuation(INTERNAL_TEMP_ADC_PIN, ADC_11db); //

	ret = true;
	sensorInit = 1;
	return ret;
}


bool TempReader::init()
{
	bool ret = initADCIfNeeded();
	uint16_t configValue = 0x120;
	SampleCenter._sensor.writeWordToReg(TMP116_REG_CONFIG, configValue);
	SamplePump._sensor.writeWordToReg(TMP116_REG_CONFIG, configValue);
	SampleLeft._sensor.writeWordToReg(TMP116_REG_CONFIG, configValue);
	SampleRight._sensor.writeWordToReg(TMP116_REG_CONFIG, configValue);

	return ret;
}

float readSampleWrap(ClosedCube::Sensor::TMP116& sensor) {
	if (sensor.isOnline()) {
		return sensor.readTemperature();
	}
	else {
		return ERROR_TEMP_READ_TIMEOUT;
	}
}

float TempReader::readPlateTmp(bool flagVerbose)
{
	return readSampleWrap(SampleCenter);
}

float TempReader::readPumpTmp(bool flagVerbose)
{
	return readSampleWrap(SamplePump);
}

float TempReader::readSampleLeftTmp(bool flagVerbose) {
	return readSampleWrap(SampleLeft);
}

float TempReader::readSampleRightTmp(bool flagVerbose) {
	return readSampleWrap(SampleRight);
}

float TempReader::readContainerTmp(bool flagVerbose)
{
	return ERROR_TEMP_READ_TIMEOUT;
}

float TempReader::readSurroundingTmp(bool flagVerbose)
{
	return ERROR_TEMP_READ_TIMEOUT;
}

float TempReader::readInternalTmp(bool flagVerbose)
{
	float tempMax = 0;
	//tempMax = max(tempMax, TECDrv1.readTemperatureC());
	//tempMax = max(tempMax, TECDrv2.readTemperatureC());
	//tempMax = max(tempMax, TECDrv3.readTemperatureC());
	//tempMax = max(tempMax, HeatDrv.readTemperatureC());
	return tempMax;
}

int TempReader::setPlateTmpCaliTmpKs(float* tmpKs, int num)
{
	return -1;
}

int TempReader::setPlateTmpCaliVolts(float* voltMvs, int num)
{
	return -1;
}

void TempReader::setLidTmpNTCBR(float ntcBVal, float referenceR)
{

}

void TempReader::setSurroundingTmpNTCBR(float ntcBVal, float referenceR)
{

}
#endif